package com.surfilter.service.impl;

import com.surfilter.bean.VirtualIdentity;
import com.surfilter.dao.VirtualMapper;
import com.surfilter.service.UploadService;
import com.surfilter.util.DateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lijia
 * @Date: Created in  2018-05-07 15:13
 * @Description:
 */
@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private VirtualMapper virtualMapper;


    @Override
    @Transactional
    public String batchImport(String account, MultipartFile file,boolean is2003) {
        List<VirtualIdentity> list = new ArrayList<VirtualIdentity>();
        int listsize =0;
        boolean notNull = false;
        InputStream  is = null;
        try {
            is = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Workbook wb = null;
        if (is2003) {
            try {
                wb = new HSSFWorkbook(is);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                wb = new XSSFWorkbook(is);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Sheet sheet = wb.getSheetAt(0);
        if(sheet!=null){
            notNull = true;
        }

        VirtualIdentity virtualIdentity;


        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if (StringUtils.isEmpty(row)){
                continue;
            }
            if (StringUtils.isEmpty(row.getCell(0).getStringCellValue()) || StringUtils.isEmpty(row.getCell(1).getStringCellValue()) || StringUtils.isEmpty(row.getCell(2).getStringCellValue())){
                continue;
            }
            virtualIdentity = new VirtualIdentity();

            row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
            String virtualIdentity1 = row.getCell(0).getStringCellValue();
            row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
            String virtualtype = row.getCell(1).getStringCellValue();
            row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
            String date = row.getCell(2).getStringCellValue();


            virtualIdentity.setAccount(account);
            virtualIdentity.setTime(DateUtil.string2DateTime(date));
            virtualIdentity.setVirtualidentity(virtualIdentity1);
            virtualIdentity.setVirtualtype(virtualtype);
            list.add(virtualIdentity);
        }


        int delcount = 0;

        if(list.size()>0){
            delcount = virtualMapper.deleteVirtualIdentityByAccount(account);
            virtualMapper.saveVirtualIdentityList(list);
        }

        listsize = list.size();
        return listsize+"-"+delcount;
    }
}

